package com.sinosoft.cnmuseum.utils;

import java.util.List;

import com.sinosoft.cnmuseum.dao.MbMusicDbrwDao;
import com.sinosoft.cnmuseum.dao.MbMusicDbrwWhrlxDao;
import com.sinosoft.cnmuseum.dao.MbMusicGcxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhrlxxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxDao;
import com.sinosoft.cnmuseum.dao.MbMusicWhsxWhrlxDao;
import com.sinosoft.cnmuseum.dao.MbMusicXzxxDao;
import com.sinosoft.cnmuseum.dao.MbMusicYxxxDao;
import com.sinosoft.cnmuseum.entity.MbMusicDbrw;
import com.sinosoft.cnmuseum.entity.MbMusicDbrwWhrlx;
import com.sinosoft.cnmuseum.entity.MbMusicGcxx;
import com.sinosoft.cnmuseum.entity.MbMusicWhrlxxx;
import com.sinosoft.cnmuseum.entity.MbMusicWhsx;
import com.sinosoft.cnmuseum.entity.MbMusicWhsxWhrlx;
import com.sinosoft.cnmuseum.entity.MbMusicXzxx;
import com.sinosoft.cnmuseum.entity.MbMusicYxxx;
import com.sinosoft.commontools.record.Record;

/**
 * @项目名称：cnmuseum
 * @类名称： MusicSaveOrUpdateUtil
 * @类描述：保存和修改数据的工具类
 * @创建时间：2019年1月11日  
 * @创建人：康仕森
 */
public class MusicSaveOrUpdateUtil {
  public int addOrMod(Record data) throws Exception {
    Integer gcxxid = data.getIntValue(MbMusicGcxx.ZDBH);
    gcxxid = addOrModGcxx(data, gcxxid);
    System.out.println(gcxxid);
    addOrModXzxx(data, gcxxid);
    Integer whrlxxxid = addOrModWhrlxxx(data, gcxxid);
    addOrModYxxx(data, gcxxid);
    addOrModWhsx(data, whrlxxxid, gcxxid);
    addOrModDbrw(data, whrlxxxid, gcxxid);
    return gcxxid;
  }


  private void addOrModDbrw(Record data, Integer whrlxxxid, Integer gcxxid) {
    String[] cols = {MbMusicDbrw.NAME,MbMusicDbrw.DBRWYEAR,MbMusicDbrw.CONTRIBUTION,MbMusicDbrw.REPRESENTATIVEWORKS};
    if (gcxxid == null || whrlxxxid == null || whrlxxxid < 1) return;
    MbMusicDbrwDao dao = new MbMusicDbrwDao();
    Record dbrw = new Record();
    String sql = "select * from " + MbMusicDbrw.MB_MUSIC_DBRW_TABLE_NAME + " where " + MbMusicDbrw.GCXXID + "=" + gcxxid;
    List<Record> list = dao.getBySql(sql);
    if (list.isEmpty()) {
      dbrw = addValue(cols, dbrw, data);
      if (dbrw != null) {
        dbrw.add(MbMusicDbrw.GCXXID, gcxxid);
        if (dao.insert(dbrw)) {
          MbMusicDbrwWhrlxDao rdao = new MbMusicDbrwWhrlxDao();
          Record r = new Record();
          r.add(MbMusicDbrwWhrlx.WHRLXID, whrlxxxid);
          r.add(MbMusicDbrwWhrlx.DBRWID, dao.getBySql(sql).get(0).getValue(MbMusicDbrw.ZDBH));
          rdao.insert(r);
        }
      }
    } else {
      dbrw = list.get(0);
      dbrw = addValue(cols, dbrw, data);
      dao.update(dbrw);
    }    

  }


  private void addOrModWhsx(Record data, Integer whrlxxxid, Integer gcxxid) {
    String[] cols = {MbMusicWhsx.INSTRUMENTTYPE,MbMusicWhsx.SONGSTER,MbMusicWhsx.LYRICIST,MbMusicWhsx.PERFORMINGFORMS,
        MbMusicWhsx.PLAYFORM,MbMusicWhsx.PERFORMER,MbMusicWhsx.GENRE,MbMusicWhsx.TOPLAYCLUB,
        MbMusicWhsx.MUSICSCOREEXPLAIN,MbMusicWhsx.MUSICSCOREIMAGE,MbMusicWhsx.INHERITTEACHEXPLAIN,MbMusicWhsx.INHERITTEACHVIDEO,
        MbMusicWhsx.SYMBOLSANDTABOOS,MbMusicWhsx.SOCIALFUNCTION,MbMusicWhsx.CULTURALROLE,MbMusicWhsx.CULTURALFUNCTION,
        MbMusicWhsx.CULTURALCONTEXT};
    if (gcxxid == null || whrlxxxid == null || whrlxxxid < 1) return;
    MbMusicWhsxDao dao = new MbMusicWhsxDao();
    Record whsx = new Record();
    String sql = "select * from " + MbMusicWhsx.MB_MUSIC_WHSX_TABLE_NAME + " where " + MbMusicWhsx.GCXXID + "=" + gcxxid;
    List<Record> list = dao.getBySql(sql);
    if (list.isEmpty()) {
      whsx = addValue(cols, whsx, data);
      if (whsx != null) {
        whsx.add(MbMusicWhsx.GCXXID, gcxxid);
        if (dao.insert(whsx)) {
          MbMusicWhsxWhrlxDao rdao = new MbMusicWhsxWhrlxDao();
          Record r = new Record();
          r.add(MbMusicWhsxWhrlx.WHRLXID, whrlxxxid);
          r.add(MbMusicWhsxWhrlx.WHSXID, dao.getBySql(sql).get(0).getValue(MbMusicWhsx.ZDBH));
          rdao.insert(r);
        }
      }
    } else {
      whsx = list.get(0);
      whsx = addValue(cols, whsx, data);
      dao.update(whsx);
    }    
  }


  private void addOrModYxxx(Record data, Integer gcxxid) {
    String[] cols = {MbMusicYxxx.PITCH,MbMusicYxxx.PITCHAUDIO,MbMusicYxxx.TONE,MbMusicYxxx.TONEAUDIO,
        MbMusicYxxx.TEMPERAMENT,MbMusicYxxx.TEMPERAMENTAUDIO,MbMusicYxxx.RANGEEXPLAIN,MbMusicYxxx.RANGEAUDIO,
        MbMusicYxxx.BESTREGISTER,MbMusicYxxx.BESTREGISTERAUDIO,MbMusicYxxx.TONEMETHOD,MbMusicYxxx.TONEMETHODAUDIO,
        MbMusicYxxx.VOICEMECHANISM,MbMusicYxxx.VOICEMECHANISMVIDEO,MbMusicYxxx.OUSTICMECHANISM,MbMusicYxxx.PROVOKE,
        MbMusicYxxx.PROVOKEVIDEO,MbMusicYxxx.STRUCT,MbMusicYxxx.STRUCTIMAGE,MbMusicYxxx.STRUCTVIDEO,
        MbMusicYxxx.SOUNDMATERIAL,MbMusicYxxx.MEDIATORMATERIAL,MbMusicYxxx.MEDIATORMATERIALIMAGE,MbMusicYxxx.MEDIATORSHAPE,
        MbMusicYxxx.MEDIATORSHAPEAUDIO,MbMusicYxxx.VIBRATIONTRANSFERMATERIAL,MbMusicYxxx.VIBRATIONTRANSFERMATERIALIMAGE,MbMusicYxxx.VIBRATIONTRANSFERSTRUCT,
        MbMusicYxxx.VIBRATIONTRANSFERSTRUCTIMAGE,MbMusicYxxx.RESONATORMATERIAL,MbMusicYxxx.RESONATORMATERIALIMAGE,MbMusicYxxx.RESONATORSTRUT,
        MbMusicYxxx.RESONATORSTRUTIMAGE,MbMusicYxxx.MAINPARTSOUND,MbMusicYxxx.MAINPARTSOUNDAUDIO,MbMusicYxxx.TECHNIQUES,
        MbMusicYxxx.TECHNIQUESVIDEO,MbMusicYxxx.SPECTRUM,MbMusicYxxx.SPECTRUMIMAGE,MbMusicYxxx.SPECTRUMANALYSIS,
        MbMusicYxxx.SPECTRUMANALYSISIMAGE,MbMusicYxxx.MEDIATORSTRUCT,MbMusicYxxx.MEDIATORSTRUCTIMAGE};
    if (gcxxid == null) return;
    MbMusicYxxxDao dao = new MbMusicYxxxDao();
    Record yxxx = new Record();
    String sql = "select * from " + MbMusicYxxx.MB_MUSIC_YXXX_TABLE_NAME + " where " + MbMusicYxxx.GCXXID + "=" + gcxxid;
    List<Record> list = dao.getBySql(sql);
    if (list.isEmpty()) {
      yxxx = addValue(cols, yxxx, data);
      if (yxxx != null) {
        yxxx.add(MbMusicYxxx.GCXXID, gcxxid);
        dao.insert(yxxx);
      }
    } else {
      yxxx = list.get(0);
      yxxx = addValue(cols, yxxx, data);
      dao.update(yxxx);
    }    
  }


  private Integer addOrModWhrlxxx(Record data, Integer gcxxid) {
    String[] cols = {MbMusicWhrlxxx.ORIGINDESCRIPTION,MbMusicWhrlxxx.ORIGINIMAGE,MbMusicWhrlxxx.ORIGINAUDIO,MbMusicWhrlxxx.ORIGINVIDEO,
        MbMusicWhrlxxx.HISTORYDESCRIPTION,MbMusicWhrlxxx.HISTORYIMAGE,MbMusicWhrlxxx.HISTORYAUDIO,MbMusicWhrlxxx.HISTORYVIDEO,
        MbMusicWhrlxxx.FOLKLOREDESCRIPTION,MbMusicWhrlxxx.FOLKLOREIMAGE,MbMusicWhrlxxx.FOLKLOREAUDIO,MbMusicWhrlxxx.FOLKLOREVIDEO,
        MbMusicWhrlxxx.FAITHDESCRIPTION,MbMusicWhrlxxx.FAITHIMAGE,MbMusicWhrlxxx.FAITHAUDIO,MbMusicWhrlxxx.FAITHVIDEO,
        MbMusicWhrlxxx.COLLECTIONPLACE,MbMusicWhrlxxx.TREASURESDESCRIPTION};
    if (gcxxid == null) return null;
    MbMusicWhrlxxxDao dao = new MbMusicWhrlxxxDao();
    Record whrlxxx = new Record();
    String sql = "select * from " + MbMusicWhrlxxx.MB_MUSIC_WHRLXXX_TABLE_NAME + " where " + MbMusicWhrlxxx.GCXXID + "=" + gcxxid;
    List<Record> list = dao.getBySql(sql);
    if (list.isEmpty()) {
      whrlxxx = addValue(cols, whrlxxx, data);
      //if (whrlxxx != null) {
        whrlxxx.add(MbMusicWhrlxxx.GCXXID, gcxxid);
        dao.insert(whrlxxx);
        return whrlxxx.getIntValue(MbMusicWhrlxxx.ZDBH);
      //} else {
      //  return null;
      //}
    } else {
      whrlxxx = list.get(0);
      whrlxxx = addValue(cols, whrlxxx, data);
      dao.update(whrlxxx);
      return whrlxxx.getIntValue(MbMusicWhrlxxx.ZDBH);
    }   
  }


  private void addOrModXzxx(Record data, Integer gcxxid) {
    String[] cols = {MbMusicXzxx.COLLECTIONCODE,MbMusicXzxx.VOLUME,MbMusicXzxx.FORMDRAWING,MbMusicXzxx.FORMDESCRIPTION,
        MbMusicXzxx.MATERIAL,MbMusicXzxx.TECHNOLOGY,MbMusicXzxx.IMPROVESTATE,MbMusicXzxx.IMPROVESTATEIMAGE};
    if (gcxxid == null) return;
    MbMusicXzxxDao dao = new MbMusicXzxxDao();
    Record xzxx = new Record();
    String sql = "select * from " + MbMusicXzxx.MB_MUSIC_XZXX_TABLE_NAME + " where " + MbMusicXzxx.GCXXID + "=" + gcxxid;
    List<Record> list = dao.getBySql(sql);
    if (list.isEmpty()) {
      xzxx = addValue(cols, xzxx, data);
      if (xzxx != null) {
        xzxx.add(MbMusicXzxx.GCXXID, gcxxid);
        dao.insert(xzxx);
      }
    } else {
      xzxx = list.get(0);
      xzxx= addValue(cols, xzxx, data);
      dao.update(xzxx);
    }    
  }


  private Integer addOrModGcxx(Record data, Integer gcxxid) throws Exception {
    String[] cols = {MbMusicGcxx.COLLECTIONCODE,MbMusicGcxx.ORGANIZATION, MbMusicGcxx.HOLDINGCODE,MbMusicGcxx.TITLE,MbMusicGcxx.TITLESELF,
        MbMusicGcxx.PHONETICSYMBOL,MbMusicGcxx.TITLEDESCRIPTION,MbMusicGcxx.CLASSHUOSA,MbMusicGcxx.CLASSBAYIN,
        MbMusicGcxx.CLASSMINJIAN,MbMusicGcxx.CLASSNEW,MbMusicGcxx.UNEARTHEDPLACE,MbMusicGcxx.YEAR,
        MbMusicGcxx.BIRTHPROCESS,MbMusicGcxx.LANGUAGE,MbMusicGcxx.USENATION,MbMusicGcxx.OTHERNATION,
        MbMusicGcxx.FAITHDESCRIPTION,MbMusicGcxx.FAITHIMAGE,MbMusicGcxx.FAITHAUDIO,MbMusicGcxx.FAITHVIDEO,
        MbMusicGcxx.COLLECTIONPLACE,MbMusicGcxx.TREASURESDESCRIPTION};
    Record gcxx = new Record();
    MbMusicGcxxDao dao = new MbMusicGcxxDao();
    if (gcxxid == null || dao.getById(gcxxid) == null) {
      gcxx = addValue(cols, gcxx, data);
      if (dao.insert(gcxx)) {
        return gcxx.getIntValue(MbMusicGcxx.ZDBH);
      } else {
        return null;
      }
    } else {
      gcxx = dao.getById(gcxxid);
      gcxx = addValue(cols, gcxx, data);
      dao.update(gcxx);
      return gcxxid;
    }
  }

  private Record addValue(String[] cols, Record record, Record data) {
    boolean valid = false;
    for (String col : cols) {
      if(StringUtils.isNoEmpty(data.getStrValue(col))){
        record.add(col, data.getStrValue(col));
      }
      if (record.getValue(col) != null) {
        valid = true;
      }
    }
    //if (valid)
      return record;
    //return null;
  }
  
  public static void main(String[] args) {
    Record r = new Record();
    r.add("organization", 1);
    r.add("holding_code", 1);
    r.add("title", 1);
    r.add("title_self", 1);
    r.add("phoneticsymbol", 1);
    r.add("titledescription", 1);
    r.add("class_huosa", 1);
    r.add("class_bayin", 1);
    r.add("class_minjian", 1);
    r.add("class_new", 1);
    r.add("unearthedPlace", 1);
    r.add("year", 1);
    r.add("birthprocess", 1);
    r.add("language", 1);
    r.add("usenation", 1);
    r.add("othernation", 1);
    r.add("volume", 1);
    r.add("formDrawing", 1);
    r.add("formDescription", 1);
    r.add("material", 1);
    r.add("technology", 1);
    r.add("improveState", 1);
    r.add("improveStateImage", 1);
    r.add("originDescription", 1);
    r.add("originImage", 1);
    r.add("originAudio", 1);
    r.add("originVideo", 1);
    r.add("historyDescription", 1);
    r.add("historyImage", 1);
    r.add("historyAudio", 1);
    r.add("historyVideo", 1);
    r.add("folkloreDescription", 1);
    r.add("folkloreImage", 1);
    r.add("folkloreAudio", 1);
    r.add("folkloreVideo", 1);
    r.add("faithDescription", 1);
    r.add("faithImage", 1);
    r.add("faithAudio", 1);
    r.add("faithVideo", 1);
    r.add("collectionPlace", 1);
    r.add("treasuresDescription", 1);
    r.add("pitch", 1);
    r.add("pitchAudio", 1);
    r.add("tone", 1);
    r.add("toneAudio", 1);
    r.add("temperament", 1);
    r.add("temperamentAudio", 1);
    r.add("range", 1);
    r.add("rangeAudio", 1);
    r.add("bestRegister", 1);
    r.add("bestRegisterAudio", 1);
    r.add("toneMethod", 1);
    r.add("toneMethodAudio", 1);
    r.add("voiceMechanism", 1);
    r.add("voiceMechanismVideo", 1);
    r.add("ousticMechanism", 1);
    r.add("provoke", 1);
    r.add("provokeVideo", 1);
    r.add("struct", 1);
    r.add("structImage", 1);
    r.add("structVideo", 1);
    r.add("soundMaterial", 1);
    r.add("mediatorMaterial", 1);
    r.add("mediatorMaterialImage", 1);
    r.add("mediatorStruct", 1);
    r.add("mediatorStructImage", 1);
    r.add("mediatorShape", 1);
    r.add("mediatorShapeAudio", 1);
    r.add("VibrationTransferMaterial", 1);
    r.add("VibrationTransferMaterialImage", 1);
    r.add("VibrationTransferStruct", 1);
    r.add("VibrationTransferStructImage", 1);
    r.add("ResonatorMaterial", 1);
    r.add("ResonatorMaterialImage", 1);
    r.add("ResonatorStrut", 1);
    r.add("ResonatorStrutImage", 1);
    r.add("MainPartSound", 1);
    r.add("MainPartSoundAudio", 1);
    r.add("techniques", 1);
    r.add("techniquesVideo", 1);
    r.add("spectrum", 1);
    r.add("spectrumImage", 1);
    r.add("SpectrumAnalysis", 1);
    r.add("SpectrumAnalysisImage", 1);
    r.add("instrumentType", 1);
    r.add("songster", 1);
    r.add("lyricist", 1);
    r.add("performingForms", 1);
    r.add("playForm", 1);
    r.add("performer", 1);
    r.add("genre", 1);
    r.add("toPlayClub", 1);
    r.add("musicScoreExplain", 1);
    r.add("musicScoreImage", 1);
    r.add("inheritTeachExplain", 1);
    r.add("inheritTeachVideo", 1);
    r.add("symbolsAndTaboos", 1);
    r.add("socialFunction", 1);
    r.add("culturalRole", 1);
    r.add("culturalFunction", 1);
    r.add("culturalContext", 1);
    r.add("name", 1);
    r.add("dbrwYear", 1);
    r.add("contribution", 1);
    r.add("representativeworks", 1);
  }
}
